package com.blog4j.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.blog4j.api.client.FeignArticle;
import com.blog4j.api.client.FeignUser;
import com.blog4j.api.vo.ArticleDateCountVo;
import com.blog4j.api.vo.CategoryArticleCountVo;
import com.blog4j.api.vo.MonthActiveUserVo;
import com.blog4j.api.vo.MonthArticleViewsVo;
import com.blog4j.common.constants.CommonConstant;
import com.blog4j.system.service.ChartService;
import com.blog4j.system.vo.resp.ChartInfoRespVo;
import com.blog4j.system.vo.resp.ContributeDateRespVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 98k灬
 * @version v1.0.0
 * @Description : 功能描述
 * @Create on : 2024/8/7 12:24
 **/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ChartServiceImpl implements ChartService {
    private final FeignArticle feignArticle;
    private final FeignUser feignUser;

    /**
     * 统计首页大屏的信息
     *
     * @return 首页大屏的信息
     */
    @Override
    public ChartInfoRespVo chartInfo() {
        // TODO 留言数
        Integer articleCount = feignArticle.articleCount();
        Integer userCount = feignUser.userCount();
        Integer organizationCount = feignUser.organizationCount();
        List<MonthActiveUserVo> monthActiveUserList = feignArticle.getMonthActiveUserList();
        List<MonthArticleViewsVo> monthArticleViewsList = feignArticle.getMonthArticleViewsList();
        List<CategoryArticleCountVo> categoryArticleCount = feignArticle.getCategoryArticleCount();

        return ChartInfoRespVo.builder()
                .articleCount(articleCount)
                .organizationCount(organizationCount)
                .messageCount(0)
                .userCount(userCount)
                .articleViewsVo(monthArticleViewsList)
                .activeUserVo(monthActiveUserList)
                .categoryArticleCountVo(categoryArticleCount)
                .build();
    }

    /**
     * 获取一年内文章贡献度
     *
     * @return 一年内文章贡献度
     */
    @Override
    public ContributeDateRespVo getContributeData() {
        ContributeDateRespVo respVo = new ContributeDateRespVo();
        List<String> yearList = this.getAllDateYear();
        List<String> contributeDate = new ArrayList<>();
        contributeDate.add(yearList.get(0));
        contributeDate.add(yearList.get(yearList.size()-1));
        respVo.setContributeDate(contributeDate);

        List<ArticleDateCountVo> articleDateCountList = feignArticle.getArticleDateCount();
        if (CollectionUtil.isEmpty(articleDateCountList)) {
            return null;
        }

        List<List<Object>> articleContributeCount = new ArrayList<>();

        for (String item : yearList) {
            List<Object> objList = new ArrayList<>();
            objList.add(item);

            List<ArticleDateCountVo> vos = articleDateCountList.stream().filter(articleDateCountVo ->
                    StringUtils.equals(item, articleDateCountVo.getDate())).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(vos)) {
                objList.add(0);
            } else {
                objList.add(vos.get(0).getCount());
            }
            articleContributeCount.add(objList);
        }
        respVo.setArticleContributeCount(articleContributeCount);
        return respVo;
    }



    // ----------------------------------private------------------------------------------

    private List<String> getAllDateYear() {
        List<String> list = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat(CommonConstant.COMPLETE_DATE_FORMAT);
        for(int i = 365; i >= 0; i--) {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, - i);
            Date d = c.getTime();
            String day = format.format(d);
            list.add(day);
        }
        return list;
    }
}
